package com.charging.controller.user;

import com.charging.entity.PaymentRecord;
import com.charging.entity.User;
import com.charging.service.PaymentService;
import com.charging.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;

@Controller
@RequestMapping("/user/balance")
public class BalanceController {

    @Resource
    private UserService userService;

    @Autowired
    private PaymentService paymentService;

    // 显示充值表单
    @GetMapping("/add")
    public String showAddForm(Model model, HttpSession session) {

        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }

        // 获取用户最近充值记录和总额
        PaymentRecord lastRecharge = paymentService.getLastRecharge(user.getId());
        Double totalRecharge = paymentService.getTotalRechargeAmount(user.getId());

        model.addAttribute("user", user);
        model.addAttribute("lastRecharge", lastRecharge);
        model.addAttribute("totalRecharge", totalRecharge);

        return "user/balance/add";
    }

    // 处理充值请求
    @PostMapping("/add")
    public String processRecharge(
            @RequestParam("amount") Double amount,
            @RequestParam("paymentMethod") String paymentMethod,
            HttpSession session,
            RedirectAttributes redirectAttributes) {

        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }

        try {
            // 验证金额
            if (amount == null || amount < 10 || amount > 10000) {
                redirectAttributes.addFlashAttribute("error", "充值金额必须在10-10000元之间");
                return "redirect:/user/balance/add";
            }

            // 更新余额
            boolean success = userService.updateBalance(user.getId(), amount);

            if (success) {
                // 记录充值记录
                PaymentRecord record = new PaymentRecord();
                record.setUserId(user.getId());
                record.setAmount(BigDecimal.valueOf(amount));
                record.setPaymentMethod(paymentMethod);
                record.setStatus("PAID");
                record.setPaymentTime(new Date());
                record.setDescription("账户充值");
                paymentService.savePaymentRecord(record);

                // 更新session中的用户信息
                user.setBalance(user.getBalance() + amount);
                session.setAttribute("user", user);

                redirectAttributes.addFlashAttribute("success", "充值成功！");
            } else {
                redirectAttributes.addFlashAttribute("error", "充值失败，请重试");
            }
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", e.getMessage());
        }

        return "redirect:/user/balance/add";
    }
}
